#ifndef RPCCLIENTAGENT_H
#define RPCCLIENTAGENT_H
#include <list>
#include <map>
#include <queue>
#include "os_thread.h"

typedef int (*func_OnDataRecievedCallBack_t)(char *buf, int buf_len);
class RPCRequest {
public:
	RPCRequest();
	virtual ~RPCRequest();

	void set_request_id(int request_id);

	char *rsp_buf;
	int rsp_buf_len;

	char *m_curr_arg_buf;
	int m_curr_arg_buf_len;

public:
	os_event_t *event;
	int request_id;
	static int m_last_request_id;
};
class RPCClientAgent
{
public:
	RPCClientAgent();
	virtual ~RPCClientAgent();
	
	int init(char *host, short port);
	static int ThreadProc(void *p);
	virtual int run();

	int request_response(RPCRequest *req, char *buf, int buf_len, int ResponseTimeout);
	int sendData(char *buf, int buf_len);
	int waitForResponse(int requestid, int timeout);

	int regeisterOnDataRecievedCallBack(int FunctionType, func_OnDataRecievedCallBack_t cb);
protected:
	int OnDataRecieved(char *buf, int buf_len);
private:
	char hostAddress[512];
	short m_port;
	int m_socket;
	os_thread_t *m_pThread;
	os_mutex_t *m_pMutex;
	std::map<int, func_OnDataRecievedCallBack_t> m_mapOnDataRecievedCallBack;
	std::map<int, RPCRequest *> m_mapRequest;

};

#endif